package com.example.demo.service.impl;


import com.example.demo.base.config.auth.UserInfoShareHolder;
import com.example.demo.constants.LanguageConstants;
import com.example.demo.mapper.LanguageMapper;
import com.example.demo.mapper.NewProblemMapper;
import com.example.demo.mapper.SubmissionMapper;
import com.example.demo.domain.entity.Language;
import com.example.demo.domain.entity.TrainingProblem;
import com.example.demo.domain.entity.TrainingSubmission;
import com.example.demo.dto.ExamSubmissionPageDto;
import com.example.demo.dto.HomeworkSubmissionPageDto;
import com.example.demo.dto.TrainingSubmissionPageDto;
import com.example.demo.message.MessageSender;
import com.example.demo.service.SubmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Capital Letters
 * @title: SubmissionService
 * @projectName oj-homework
 * @description: 代码提交服务层
 * @date 2022/5/2418:44
 */
@Service
@Transactional
public class SubmissionServiceImpl implements SubmissionService {

    @Autowired
    private NewProblemMapper newProblemMapper;

    @Autowired
    private SubmissionMapper submissionMapper;

    @Autowired
    private LanguageMapper languageMapper;

    @Autowired
    private MessageSender messageSender;

    /**
     * 获取指定时间内提交的数量, 并按天数汇总.
     * @param  startTime - 统计起始时间
     * @param  endTime - 统计结束时间
     * @param  uid - 用户的唯一标识符
     * @param  isAcceptedOnly - 是否只统计通过的提交记录
     * @return 包含日期和提交次数的键值对 Map
     */
    @Override
    public List<Map<String, Object>> getNumberOfSubmissionsGroupByDay(Date startTime, Date endTime, long uid, boolean isAcceptedOnly) {


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        String startTimeString = sdf.format(startTime);
        String endTimeString = sdf.format(endTime);
        List<Map<String, Object>> submissions = submissionMapper.getNumberOfSubmissionsGroupByDay(startTimeString, endTimeString, uid, isAcceptedOnly);

        return submissions;
    }





    /**
     * 获取最新的评测记录列表.
     * 用于定时获取最新的评测记录.
     * @param examName - 考试名称
     * @param userName - 用户的用户名
     * @param problemName - 试题名称
     * @param page - 每次加载评测记录的页数
     * @param size - 每页加载评测记录的数量
     * @return 试题列表(List<Submission>对象)
     */
    @Override
    public ExamSubmissionPageDto getExamSubmissionRecord(String examName, String userName, String problemName, int page, int size) {
        long offset = page * size;
        if ( offset < 0 ) {
            offset = 0;
        }
        ExamSubmissionPageDto examSubmissionPageDto = new ExamSubmissionPageDto();
        examSubmissionPageDto.setList(submissionMapper.getExamSubmissionRecord(examName, userName, problemName, offset, size));
        examSubmissionPageDto.setTotal(submissionMapper.getExamSubmissionRecordCount(examName, userName, problemName));
        return examSubmissionPageDto;
    }

    /**
     * 获取作业系统的提交记录.
     * @param className - 班级名称
     * @param userName - 用户的用户名
     * @param problemName - 试题名称
     * @param page - 每次加载评测记录的页数
     * @param size - 每页加载评测记录的数量
     * @return 某个范围内的所有提交记录
     */
    @Override
    public HomeworkSubmissionPageDto getHomeworkSubmissionRecord(String className, String userName, String problemName, int page, int size) {
        long offset = page * size;
        if ( offset < 0 ) {
            offset = 0;
        }
        HomeworkSubmissionPageDto homeworkSubmissionPageDto = new HomeworkSubmissionPageDto();
        homeworkSubmissionPageDto.setList(submissionMapper.getHomeworkSubmissionRecord(className, userName, problemName, offset, size));
        homeworkSubmissionPageDto.setTotal(submissionMapper.getHomeworkSubmissionRecordCount(className, userName, problemName));
        return homeworkSubmissionPageDto;
    }

}
